const express = require('express');
// 创建一个路由
const router = express.Router();
const mysql = require('../conn');

// 查询用户
router.get('/user', (req, res) => {
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from user`;

    mysql.query(sql, (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// 根据id查询用户
router.get('/info', (req, res) => {
    let { user_id } = req.query;
    let obj = {};


    let sql = `select * from user where user_id = ?`;
    mysql.query(sql, [user_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);

    });
})

// 新增用户
router.post('/add', (req, res) => {
    let { user_name, sex, birthday, profession, city, intro, account, level } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `insert into user (user_name,sex,birthday,profession,city,intro,account,level) value (?,?,?,?,?,?,?,?)`;

    mysql.query(sql, [user_name, sex, birthday, profession, city, intro, account, level], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);

    });
});

// 删除

router.put('/delete', (req, res) => {
    let { user_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `delete from user where user_id in (?)`;

    mysql.query(sql, [user_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        // obj.data = results;
        res.send(obj);
    });
});

// 修改
router.put('/update', (req, res) => {
    let { user_name, sex, birthday, profession, city, intro, account, level, user_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `update user set user_name=?,sex=?,birthday=?,profession=?,city=?,intro=?,account=?,level=? where user_id in (?);`;

    mysql.query(sql, [user_name, sex, birthday, profession, city, intro, account, level, user_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// 登录
router.post('/login', (req, res) => {
    let { account, password } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `select * from user where account = ?`;

    mysql.query(sql, [account], (err, results) => {
        if (err) return console.log(err);
        if (results.some(item => item.account === account)) {
            if (results.some(item => JSON.parse(JSON.stringify(item)).password == password)) {
                // console.log("jsonObject 存在于数组中");  
                obj.code = 0;
                obj.desc = '登录成功';
                obj.data = results;

            } else {
                // console.log("jsonObject 不存在于数组中");  
                obj.code = 2;
                obj.desc = '密码错误';
            }
        } else {
            obj.code = 3;
            obj.desc = '用户不存在';
        }

        res.send(obj);
    });
});
// 注册
router.post('/register', (req, res) => {
    let { account, password } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    mysql.query("select account from user", (err, results1) => {
        if (results1.some(item => item.account === account)) {
            obj.code = 1;
            obj.desc = '用户已注册过，不能重复注册';
            res.send(obj);
        } else {
            let sql = `insert into user (account,password) value (?,?)`;
            mysql.query(sql, [account, password], (err, results) => {
                if (err) return console.log(err);

                obj.code = 0;
                obj.desc = '注册成功';
                obj.data = results;
                res.send(obj);
            });
        }
    })
});
// 关注得到博主信息
router.get('/follow', (req, res) => {
    let { user_id } = req.query;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `select * from follow_fan where user_id in (?)`;
    let follow_ids = [];
    mysql.query(sql, [user_id], (err, results) => {
        if (err) return console.log(err);
        results.forEach(item => {
            follow_ids.push(item.follow_id)  ;
        });
        obj.code = 200;
        obj.desc = '成功';
        obj.data = [];

        for(let i =0;i<follow_ids.length;i++){
            mysql.query(`select * from user where user_id in (?)`, [follow_ids[i]], (err, results) => {
                if (err) return console.log(err);
               
                obj.data.push(results[0]);
                console.log(obj);
            })
        }
        setTimeout(() => {
            
            res.send(obj);
            
        }, 1000);

        
    });
})
// 粉丝
router.get('/fan', (req, res) => {
    let { user_id } = req.query;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `select * from follow_fan where follow_id in (?)`;
    let fan_ids = [];
    mysql.query(sql, [user_id], (err, results) => {
        if (err) return console.log(err);
        results.forEach(item => {
            fan_ids.push(item.user_id)  ;
        });
        obj.code = 200;
        obj.desc = '成功';
        obj.data = [];

        for(let i =0;i<fan_ids.length;i++){
            mysql.query(`select * from user where user_id in (?)`, [fan_ids[i]], (err, results) => {
                if (err) return console.log(err);
               
                obj.data.push(results[0]);
                console.log(obj);
            })
        }
        setTimeout(() => {
            
            res.send(obj);
            
        }, 1000);

        
    });
})
//统计关注和粉丝数
router.get('/statistics', (req,res)=>{
    let { user_id } = req.query;
    let obj = {};
    let sql = `select count(case when user_id in (?) then 1 end) as followCount,count(case when follow_id in (?) then 1 end) as fanCount from follow_fan`;
    mysql.query(sql, [user_id, user_id], (err,results)=>{
        if(err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    })
})

// 关注接口
router.post('/follow', (req, res) => {
    let { user_id, follow_id } = req.body;
    // let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句

    let sql = `insert into follow_fan (user_id,follow_id) value (?,?)`;

    mysql.query(sql, [user_id, follow_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = {
            "id": results.insertId
        }
        res.send(obj);
    });
});

// 取消点赞接口
router.post('/cancelfollow', (req, res) => {
    let { id } = req.body;
    // let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    let like_num;
    let sql = `delete from follow_fan where id in (?)`;
    // 数据库的增加语句
    mysql.query(sql, [id], (err, result) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        res.send(obj);

    })


});

module.exports = router;